package com.group2.demo_country_group2;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.widget.Toast;

public class CountryData implements ICountryData{
	
	private SQLiteDatabase openDatabase()
	{
		SQLiteDatabase db = null;
		String SDcardPath = Environment.getExternalStorageDirectory().getPath();
		String myDbPath = SDcardPath + "/" + "mydatabase.sqlite";
		//textView1.setText("DB Path:" + myDbPath);
		try {
			db = SQLiteDatabase.openDatabase(myDbPath, null,
					SQLiteDatabase.CREATE_IF_NECESSARY);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return db;
	}
	public List<MyCountry> getallData() {
		List<MyCountry>countries = new ArrayList<MyCountry>();	
		SQLiteDatabase db = openDatabase();
		try {
			String [] columns = {"recID", "NameEN", "NameVi","Image","Population"};
			Cursor c = db.query("ListView", columns, null, null, null, null, "recID");
			//int number_row = c.getCount();
			int idCol = c.getColumnIndex("recID");
			int NameEnCol = c.getColumnIndex("NameEN");
			int NameViCol = c.getColumnIndex("NameVi");
			int NameImage = c.getColumnIndex("Image");
			int NamePopulation = c.getColumnIndex("Population");
			while (c.moveToNext()) {
				MyCountry temp = new MyCountry();
				temp.recID = c.getInt(idCol);
				temp.NameEn = c.getString(NameEnCol);
				temp.NameVi = c.getString(NameViCol);
				temp.Image = c.getBlob(NameImage);
				temp.Population = c.getDouble(NamePopulation);
				countries.add(temp);
			}
		} catch (Exception e) {
		}
		return countries;
	}
}

